package com.example.himalaya.base;

import android.app.Application;
import android.content.Context;
import android.os.Handler;

import androidx.annotation.NonNull;

import com.example.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.DeviceInfoProviderDefault;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDeviceInfoProvider;
import com.ximalaya.ting.android.opensdk.player.XmPlayerManager;
import com.ximalaya.ting.android.opensdk.util.BaseUtil;
import com.ximalaya.ting.android.opensdk.util.SharedPreferencesUtil;

/**
 * Application父类，入口类
 */
public class BaseApplication extends Application implements DemoHelper.AppIdsUpdater {
    private static final String KEY_LAST_OAID = "last_oaid";
    private String oaid;

    private static Handler sHandler = null;

    private static Context sContext=null;

    @Override
    public void onCreate() {
        super.onCreate();
        if (BaseUtil.isMainProcess(this)) {
            oaid = SharedPreferencesUtil.getInstance(getApplicationContext()).getString(KEY_LAST_OAID);
            new DemoHelper(this).getDeviceIds(getApplicationContext());
            //配置appkey appSecret
            CommonRequest mXimalaya = CommonRequest.getInstanse();
            if (DTransferConstants.isRelease) {
                String mAppSecret = "8646d66d6abe2efd14f2891f9fd1c8af";
                mXimalaya.setAppkey("9f9ef8f10bebeaa83e71e62f935bede8");
                mXimalaya.setPackid("com.app.test.android");
                mXimalaya.init(this, mAppSecret, true, getDeviceInfoProvider(this));
            } else {
                String mAppSecret = "0a09d7093bff3d4947a5c4da0125972e";
                mXimalaya.setAppkey("f4d8f65918d9878e1702d49a8cdf0183");
                mXimalaya.setPackid("com.ximalaya.qunfeng");
                mXimalaya.init(this, mAppSecret, getDeviceInfoProvider(this));
            }
        }

        //初始化LogUtil
        LogUtil.init(this.getPackageName(), false);

        //初始化播放器
        XmPlayerManager.getInstance(this).init();

        sHandler = new Handler();

        sContext = getBaseContext();
    }


    /**
     * 提供主线程，更新UI
     * @return
     */
    public static Handler getHandler() {
        return sHandler;
    }

    /**
     * 提供全局Content,没有Content的类可以调用
     * @return
     */
    public static Context getAppContext() {
        return sContext;
    }

    public IDeviceInfoProvider getDeviceInfoProvider(Context context) {
        return new DeviceInfoProviderDefault(context) {
            @Override
            public String oaid() {
                return oaid;
            }
        };
    }

    @Override
    public void OnOaidAvalid(@NonNull String ids) {
        oaid = ids;
        SharedPreferencesUtil.getInstance(getApplicationContext()).saveString(KEY_LAST_OAID, ids);
        System.out.println("TingApplication.OnOaidAvalid  " + ids);
    }

}
